﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using Jsptpd.Common.dataInital;

namespace Jsptpd.Common.xmlUtil
{
    public class XmlConverter
    {

        #region Attribute
        private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(XmlConverter));  
        #endregion


        public static DataTable ReadDocumentToDataTable(string xmlString)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(xmlString);
            //Read multi node data
            XmlNodeList nodes = xmlDocument.SelectNodes("/list/cn.myapps.webservice.model.SimpleDocument/items");
            DataStructureConstractor datastructureConstractor = new DataStructureConstractor();
            DataTable dataTable = datastructureConstractor.constructPrjinfoTable();
            int dataTableRowNum = 0;
            foreach (XmlNode item in nodes)
            {
                DataRow dr = dataTable.NewRow();
                dr["编号"] = dataTableRowNum++;
                foreach (XmlNode entryLevNode in item.SelectNodes("entry"))
                {
                    foreach (XmlNode entryStrNode in entryLevNode.SelectNodes("string"))
                    {
                        try
                        {
                            XmlConverter.updateNodeToDatatable(entryStrNode, ref dr);
                        }
                        catch (Exception ex)
                        {
                            logger.Error("Insert into datatable encount an error,the detail is:" + ex.Message);
                        }
                    }
                }
                dataTable.Rows.Add(dr);
            }
            return dataTable;
        }



        public static DataTable ReadAllFieldDocumentToDataTable(string xmlString)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(xmlString);
            //Read multi node data
            XmlNodeList nodes = xmlDocument.SelectNodes("/list/cn.myapps.webservice.model.SimpleDocument/items");
            DataStructureConstractor datastructureConstractor = new DataStructureConstractor();
            DataTable dataTable = datastructureConstractor.constructAllPrjinfoTable();
            int dataTableRowNum = 0;
            foreach (XmlNode item in nodes)
            {
                DataRow dr = dataTable.NewRow();
                dr["编号"] = dataTableRowNum++;
                foreach (XmlNode entryLevNode in item.SelectNodes("entry"))
                {
                    foreach (XmlNode entryStrNode in entryLevNode.SelectNodes("string"))
                    {
                        try
                        {
                            XmlConverter.updateNodeToAllDatatable(entryStrNode, ref dr);
                        }
                        catch (Exception ex)
                        {
                            logger.Error("Insert into datatable encount an error,the detail is:" + ex.Message);
                        }
                    }
                }
                dataTable.Rows.Add(dr);
            }
            return dataTable;
        }



        public static void updateNodeToDatatable(XmlNode node, ref DataRow dr)
        {

            switch (node.InnerText)
            {
                case "项目名称":
                    dr["项目名称"] = node.NextSibling.InnerText;
                    break;
                case "项目编号":
                    dr["项目编号"] = node.NextSibling.InnerText;
                    break;
                case "项目类型":
                    dr["项目类型"] = node.NextSibling.InnerText;
                    break;
                case "主办部门":
                    dr["主办部门"] = node.NextSibling.InnerText;
                    break;
                case "项目负责人":
                    dr["项目负责人"] = node.NextSibling.InnerText;
                    break;
                case "联系人":
                    dr["联系人"] = node.NextSibling.InnerText;
                    break;
                case "联系人办公电话":
                    dr["联系人办公电话"] = node.NextSibling.InnerText;
                    break;
                case "客商名称":
                    dr["客商名称"] = node.NextSibling.InnerText;
                    break;
                default:
                    logger.Error(node.InnerText + ",No matching field!!!!!");
                    break;
            }
        }


        public static void updateNodeToAllDatatable(XmlNode node, ref DataRow dr)
        {

            switch (node.InnerText)
            {
                case "项目名称":
                    dr["项目名称"] = node.NextSibling.InnerText;
                    break;
                case "项目编号":
                    dr["项目编号"] = node.NextSibling.InnerText;
                    break;
                case "项目类型":
                    dr["项目类型"] = node.NextSibling.InnerText;
                    break;
                case "主办部门":
                    dr["主办部门"] = node.NextSibling.InnerText;
                    break;
                case "项目负责人":
                    dr["项目负责人"] = node.NextSibling.InnerText;
                    break;
                case "联系人":
                    dr["联系人"] = node.NextSibling.InnerText;
                    break;
                case "联系人办公电话":
                    dr["联系人办公电话"] = node.NextSibling.InnerText;
                    break;
                case "客商名称":
                    dr["客商名称"] = node.NextSibling.InnerText;
                    break;
                case "表单编号":
                    dr["表单编号"] = node.NextSibling.InnerText;
                    break;
                case "立项方式":
                    dr["立项方式"] = node.NextSibling.InnerText;
                    break;
                default:
                    logger.Error("No matching field!!!!!");
                    break;
            }
        }

    }
}
